﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.SqlClient;
using System.Linq;
using System.Linq.Dynamic;
using System.Transactions;
using PagedList;
using Sovigaz.DataProvider;
using Utilities;

namespace Sovigaz.Permission.Repositories
{
    public class CommandLineRepository : GenericRepository
    {
        public CommandLineRepository()
            : base("CommandLineID")
        {
        }

        public ICollection<CommandLine> CommandLines { get; set; }

        public int Update(CommandLine Command)
        {
            var update = new CommandLine();

            using (var scope = new TransactionScope())
            {
                if (Command.CommandLineID > 0)
                    update = GetByID(Command.CommandLineID);

                update.CommandLineID = Command.CommandLineID;
                update.PlanQty = Command.PlanQty;
                update.ShippedQty = Command.ShippedQty;
                update.ReceivedFullQty = Command.ReceivedFullQty;
                update.ReceivedEmptyQty = Command.ReceivedEmptyQty;
                update.Description = Command.Description;

                if (update.CommandLineID == 0)
                    _context.CommandLines.Add(update);

                _context.SaveChanges();

                scope.Complete();
            }

            _context.Entry(update).Reload();

            return update.CommandLineID;
        }

        public CommandLine GetByID(int id)
        {
            return _context.CommandLines.FirstOrDefault(p => p.CommandLineID == id);
        }

        public List<CommandLine> GetByCommandID(int id)
        {
            List<CommandLine> commandLines = _context.CommandLines.Where(p => p.CommandID == id).ToList();
            int partnerID;
            //Get Detail
            for (int i = 0; i < commandLines.Count; i++)
            {
                partnerID = commandLines[i].PartnerID;
                commandLines[i].PartnerName = _context.Partners.FirstOrDefault(p => p.PartnerID == partnerID).PartnerName;
            }
            return commandLines;
        }

        public CommandLineRepository GetList(string whereClause, string orderBy, string orderDirection, int pageNumber)
        {
            var Commands = _context.CommandLines.Where(whereClause)
                .OrderBy(String.Format("{0} {1}", orderBy.Trim(), orderDirection.Trim()))
                .ToPagedList(pageNumber, DefaultPageSize);

            TotalItemCount = Commands.TotalItemCount;
            TotalItemOnPage = Commands.Count;
            PageCount = Commands.PageCount;
            PageNumber = Commands.PageNumber;
            CommandLines = Commands.ToList();

            return this;
        }

        public List<CommandLine> GetList(int? CommandID)
        {
            if (CommandID.HasValue && CommandID > 0)
                return _context.CommandLines.Where(p => p.CommandID == CommandID).ToList();
            else return new List<CommandLine>();
        }

        public void Update(DeliveryPlan deliveryPlan, int CommandID, TransactionScope transactionScope)
        {
            List<int> list1s = new List<int>();
            List<int> list2s = new List<int>();
            foreach (var item in deliveryPlan.DeliveryPlanLines)
            {
                int Qty = item.PlanQty;
                int OrderID = _context.OrderLines.FirstOrDefault(o => o.OrderLineID == item.OrderLineID).OrderID;
                int PartnerID = _context.Orders.FirstOrDefault(o => o.OrderID == OrderID).PartnerID;
                if (list1s.Contains(PartnerID))
                {
                    list2s[list1s.IndexOf(PartnerID)] += Qty;
                    continue;
                }
                list1s.Add(PartnerID);
                list2s.Add(Qty);
            }
            for (int i = 0; i < list1s.Count; i++)
            {
                var update = new CommandLine();
                update.CommandID = CommandID;
                update.PartnerID = list1s[i];
                update.PlanQty = list2s[i];
                update.Status = "N";
                if (update.CommandLineID <= 0)
                    _context.CommandLines.Add(update);
            }
            _context.SaveChanges();
        }
    }
}
